package control.client;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import control.server.FilmesControl;

/**
 * Servlet implementation class FilmeServlet
 */
public class FilmeServletBusca extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private FilmesControl control;

	public FilmeServletBusca() {
		super();
		try {
			control = new FilmesControl();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		//

		try {
			String opcao = request.getParameter("opcao");
			String pag = request.getParameter("pag");

			if (pag.isEmpty() || pag.equalsIgnoreCase("")) {
				pag = "1";
			}
			
			if (opcao.equalsIgnoreCase("lancamentos")) {
				request.getSession().setAttribute("opcao", "lancamentos");
				montaPagina("data_lanc_filme", pag, request, response);
				
			}else if(opcao.equalsIgnoreCase("mais_locados")){
				request.getSession().setAttribute("opcao", "mais_locados");
				montaPagina("qtd_locacao", pag, request, response);
				
			}else if(opcao.equalsIgnoreCase("nome_filme")){
				request.getSession().setAttribute("opcao", "nome_filme");
				montaPagina("nome_filme", pag, request, response);
			}else{
				request.getSession().setAttribute("opcao", opcao);
				montaPagina(opcao, pag, request, response);
			}
			


		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void montaPagina(String opcao, String pagina, HttpServletRequest request,	HttpServletResponse response) throws Exception {

		int pag = Integer.parseInt(pagina);
		int filmes_por_pag = 6;
		int total = control.total();

		int num_total_paginas = (int) ((total % filmes_por_pag == 0) ? total/ filmes_por_pag : Math.floor(total / filmes_por_pag) + 1);

		int ant = pag - 1;
		int pos = pag + 1;

		if (ant == 0)ant = 1;
		if (pos > num_total_paginas)pos = num_total_paginas;

		request.getSession().setAttribute("pag", pag);
		request.getSession().setAttribute("ant", ant);
		request.getSession().setAttribute("pos", pos);
		request.getSession().setAttribute("total_pag", num_total_paginas);
		

		int inicio = (pag * filmes_por_pag) - filmes_por_pag;
		
		String nome = request.getParameter("nome");

		if (nome.equalsIgnoreCase("sim")) {
			request.getSession().setAttribute("busca",control.listaNome(opcao, inicio, filmes_por_pag));
		}else{
			request.getSession().setAttribute("busca",control.lista(opcao, inicio, filmes_por_pag));
		}

		//request.getSession().setAttribute("busca",control.lista(opcao, inicio, filmes_por_pag));

		request.getRequestDispatcher("/busca.jsp").forward(request, response);

	}

}
